package com.space.school.dao.impl;

import com.space.school.dao.CategoryDao;
import com.space.school.domain.Category;
import com.space.school.domain.User;
import com.space.school.utils.SpaceJDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.*;

public class CategoryDaoImpl implements CategoryDao {
    private JdbcTemplate jdbcTemplate = new JdbcTemplate(SpaceJDBCUtils.getDs());

    @Override
    public List<Category> search(int start, int pageSize, Map<String, String[]> map) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("select * from product_category where 1=1 ");
        ArrayList<Object> params = new ArrayList<>();
//        取map键中的数组
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            if (!map.get(key)[0].equals("") && !key.equals("currentPage")) {
                stringBuffer.append("and " + key + " like ?");
                params.add("%" + map.get(key)[0] + "%");
            }
        }
        params.add(start);
        params.add(pageSize);
        stringBuffer.append(" order by category_id limit ?,? ");
        return jdbcTemplate.query(stringBuffer.toString(), new BeanPropertyRowMapper<Category>(Category.class), params.toArray());
    }

    @Override
    public int count(Map<String, String[]> map) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("select count(*) from product_category where 1=1 ");
        ArrayList<Object> params = new ArrayList<>();
//        取map键中的数组
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            if (!map.get(key)[0].equals("") && !key.equals("currentPage")) {
                stringBuffer.append("and " + key + " like ?");
                params.add("%" + map.get(key)[0] + "%");
            }
        }
        return jdbcTemplate.queryForObject(stringBuffer.toString(), Integer.class, params.toArray());
    }

    @Override
    public int insert(String category_name, String category_type) {
        String sql = "insert into product_category(category_name,category_type,create_time) values(?,?,?)";
        int result = jdbcTemplate.update(sql, category_name, category_type, new Date());
        return result;
    }

    @Override
    public Category isInsert(String category_name) {
        String sql = "select * from product_category where category_name=?";
//        sql行的映射器  将sql 查询后的结果映射到集合中
        List<Category> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Category>(Category.class), category_name);
        if (list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }

    @Override
    public void delete(int category_id) {
        String sql = "delete from product_category where category_id = ?";
        jdbcTemplate.update(sql, category_id);
    }

    @Override
    public void update(Category category)  {
        String sql = "UPDATE product_category SET category_name = ?,category_type=?,update_time=? WHERE category_id = ? ";
        jdbcTemplate.update(sql, category.getCategory_name(), category.getCategory_type(),new Date(), category.getCategory_id());
    }

    @Override
    public Category findById(int category_id) {
        String sql = "select * from product_category where category_id=?";
        List<Category> category = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Category>(Category.class), category_id);
        if (category.size() > 0) {
            return category.get(0);
        }
        return null;
    }
}
